Placing a Block (function)
Uses This function can be very useful for items like sugar cane. You can't edit the way a block is placed, so you can instead use an item to place the block. You do not need this function for doors, the reason for this is explained in the doors section. The Simple Way The simplest way to make an item place a block is to use the player.placeBlock function. For example: rightclick="player.placeBlock(origin, 32, 0, side);"; This will place a block in the way that you would with a normal block in hand, being placed adjacent to the side you're looking at in an open space. This is, of course, just the easy way and not the most ideal in all circumstances. Different Ways to Code it This method simply allows the player to place a block by right-clicking the ground. This will change the space above the rightclicked block to a dirt block even if a block already exists. This method will also always place the block above the the right clicked location, even if the player had right-clicked from the side. rightclicked="origin.y++; world.setBlockId(origin, 3);"; This method will change the block above the selected location to be a colored wool. rightclicked="origin.y++; world.setBlockIdAndMetadata(origin, 35, 1);"; This would set the damage value of the block; if it was wool it would change it to Magenta Wool, if it was wood it would change it to Birch Wood, etc. rightclicked="origin.y++; world.setBlockMetadata(origin, 2);"; The reason we put "rightclicked" is because that is the "trigger" for this function. "origin.y++;" makes it so the function affects the block above it. "world.setBlockId(origin, 3)" Let's split this. "world" is the category. "setBlockId" is the function, what will happen. "origin, 3" are the arguments, Position pos and int id, meaning where and what. You can also use the following events in place of rightclicked: explode (grenade), blockdestroyed, or hitmob. Earlier, it was stated that the above methods will simply replace the block. If you want your item to only be placed if there is room to place it, An if statement is necessary to check if there is space. For now, the code below will only allow the block to be placed if the location is air. It will not replace water or lava. rightclicked="origin.y++;if(world.getBlockId(origin) 0){world.setBlockId(origin, 5);}"; This version only places a wooden planks above the rightclicked location if there is nothing occupying the desination. Making the Item Finite If you have been using the scripts above, you may have noticed that the item used to place blocks can be used infinitely. There are severals ways to change this to allow an item to run out and disappear. The first that shall be covered uses a method similar to how sugar cane is placed. For these examples, 448 will be considered the item id that was used to place the block. rightclicked="origin.y++;world.setBlockId(origin, 5);player.removeFromInventory(448, 1, 0);"; The removeFromInventory will remove 1 item from the players inventory with the id of 448 and 0 damage. The next example will be for an item that places blocks in exchange for damaging itself. If this method is used, you must remember to set a value for the maxuses attribute before using this code. It is also a good idea to make a tool with this code also set maxstack to 1 so that non-damaged tools do not stack with a near broken one. rightclicked="origin.y++;world.setBlockId(origin, 5);player.damageItem(player.getCurrentSlot(), 1);"; This method will damage the item once in exchange for placing a free wooden plank above selected location. The amount of uses for this version is determined by the value set my maxuses and how much you damage the item per block placed. Limiting Where Blocks are Placed One of the best uses for limiting how a block can be placed is that you can also include conditional statements to restrict the placement of the block to certain areas. An simple example for this is that netherwart, a plant from the nether, can only be placed on soul sand. To imitate this, you can use code similar to below to restrict block placement. rightclick = "if(world.getBlockId(origin) 88){origin.y++;world.setBlockId(origin, 115);player.removeFromInventory(372, 1, 0);}"; This rightclick event will first check if you have right clicked on soul sand(88). If that is true, it will proceed to change the block above the soulsand to a netherwart block(115) and will remove one netherwart item(372) from the player's inventory. Of course, if you want to have multiple blocks your item can be used on, you have to include OR(||) statements in your if condition like below. rightclick = "if(world.getBlockId(origin) 2 || world.getBlockId(origin) 3){origin.y++;world.setBlockId(origin, 171);player.removeFromInventory(448, 1, 0)}"; This version will check if the right clicked block is either dirt or grass before placing the block with id 171 on top of it. If it successfully placed, one of the item 448 will be removed from the players inventory. Category:Scripting